package com.qf.git.servlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;

@WebServlet("/countip")

public class CountIP extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        final InputStream resourceAsStream = getClass().getResourceAsStream("/i.txt");
        final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resourceAsStream));
        HashMap<String, Integer> map = new HashMap<>();
        String len;
        while ((len = bufferedReader.readLine()) != null) {
            if (!map.containsKey(len)) {
                map.put(len, 1);
            } else {
                map.put(len, map.get(len) + 1);
            }
        }
        for (String key : map.keySet()) {
            Integer val = map.get(key);
            resp.getWriter().print("ip:" + key + "，有" + val + "个<br>");
        }
    }
}